home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / P Examples / delay.p < prev    next >
Encoding:
Text File  |  1994-08-28  |  4.5 KB  |  208 lines  |  [TEXT/PJMM]

  1. unit aCommand;
  2.  
  3. {  ================================================== }
  4.  
  5. { delay.p - an examle of a simple nShell (tm) command. }
  6.  
  7. { Copyright ( c ) 1994 Newport Software Development }
  8.  
  9. { You may distribute unmodified copies of this file for noncommercial }
  10. { purposes . You may use this file as a reference when writing your }
  11. { own nShell ( tm ) commands . }
  12.  
  13. { All other rights are reserved . }
  14.  
  15. {  ================================================== }
  16.  
  17. interface
  18.  
  19. { The NSHC unit defines the interfaces nshell to callbacks.  See "nshc.p" }
  20.  
  21.     uses
  22.         NSHC;
  23.  
  24.     type
  25.  
  26.         t_target = LONGINT;
  27.         t_targetP = ^t_target;
  28.         t_targetH = ^t_targetP;
  29.  
  30. { The routine "theCommand" is called by the nShell.lib to to the work of the command. }
  31.  
  32.     procedure theCommand (nshc_parms: t_nshc_parms; nshc_calls: t_nshc_calls);
  33.  
  34. {  ================================================== }
  35.  
  36. implementation
  37.  
  38. {  ================================================== }
  39.  
  40. { utility commands }
  41.  
  42.     function got_option (nshc_parms: t_nshc_parms; option: char): boolean;
  43.         var
  44.             found: boolean;
  45.             arg, argc: integer;
  46.             i: integer;
  47.             c: char;
  48.     begin
  49.         found := false;
  50.  
  51.         arg := 1;
  52.         argc := nshc_parms^.argc;
  53.  
  54.         while not found and (arg < argc) do
  55.             begin
  56.                 i := nshc_parms^.argv[arg];
  57.                 c := nshc_parms^.arg_buf[i];
  58.                 if c = '-' then
  59.                     while (c <> chr(0)) and not found do
  60.                         begin
  61.                             i := succ(i);
  62.                             c := nshc_parms^.arg_buf[i];
  63.                             found := c = option;
  64.                         end;
  65.                 arg := succ(arg);
  66.             end;
  67.  
  68.         got_option := found;
  69.  
  70.     end;
  71.  
  72. {  ================================================== }
  73.  
  74.     function get_num (nshc_parms: t_nshc_parms; arg: integer; var num: longint): boolean;
  75.         var
  76.             ok: boolean;
  77.             i: integer;
  78.             c: char;
  79.     begin
  80.  
  81.         ok := true;
  82.         num := 0;
  83.         i := nshc_parms^.argv[arg];
  84.  
  85.         repeat
  86.             c := nshc_parms^.arg_buf[i];
  87.             if (c <> chr(0)) then
  88.                 if (c >= '0') and (c <= '9') then
  89.                     num := (10 * num) + ord(c) - ord('0')
  90.                 else
  91.                     ok := false;
  92.             i := succ(i);
  93.         until not ok or (c = chr(0));
  94.  
  95.         get_num := ok;
  96.  
  97.     end;
  98.  
  99. {  ================================================== }
  100.  
  101. { State Machine - Start }
  102.  
  103.     procedure DelayStart (nshc_parms: t_nshc_parms; nshc_calls: t_nshc_calls);
  104.         var
  105.             timeH: t_targetH;
  106.             minutes: boolean;
  107.             usage: boolean;
  108.             arg, argc: integer;
  109.             s: Str255;
  110.             num: longint;
  111.     begin
  112.         usage := false;
  113.         minutes := got_option(nshc_parms, 'm');
  114.         argc := nshc_parms^.argc;
  115.         if (argc = 2) or ((argc = 3) and minutes) then
  116.             begin
  117.                 arg := 1;
  118.                 if nshc_parms^.arg_buf[nshc_parms^.argv[arg]] = '-' then
  119.                     arg := 2;
  120.                 if arg >= argc then
  121.                     usage := true
  122.                 else if get_num(nshc_parms, arg, num) then
  123.                     begin
  124.                         timeH := t_targetH(newHandle(SIZEOF(LONGINT)));
  125.                         num := num * 60;
  126.                         if minutes then
  127.                             num := num * 60;
  128.                         timeH^^ := TickCount + num;
  129.                         nshc_parms^.data := Handle(timeH);
  130.                         nshc_parms^.action := nsh_continue;
  131.                     end
  132.                 else
  133.                     usage := true;
  134.             end
  135.         else
  136.             usage := true;
  137.  
  138.         if usage then
  139.             begin
  140.                 s := 'Usage: delay delay_time [-m]';
  141.                 NSH_putStr_err(nshc_calls, s);
  142.                 NSH_putchar_err(nshc_calls, RETURN_CHAR);
  143.                 nshc_parms^.action := nsh_idle;
  144.                 nshc_parms^.result := NSHC_ERR_PARMS;
  145.             end;
  146.     end;
  147.  
  148. {  ================================================== }
  149.  
  150. { State Machine - Stop }
  151.  
  152.     procedure DelayStop (nshc_parms: t_nshc_parms; nshc_calls: t_nshc_calls);
  153.         var
  154.             timeH: Handle;
  155.     begin
  156.         timeH := nshc_parms^.data;
  157.         if timeH <> nil then
  158.             DisposeHandle(timeH);
  159.         nshc_parms^.action := nsh_idle;
  160.         nshc_parms^.result := 0;
  161.     end;
  162.  
  163. {  ================================================== }
  164.  
  165. { State Machine - Continue }
  166.  
  167.     procedure DelayContinue (nshc_parms: t_nshc_parms; nshc_calls: t_nshc_calls);
  168.         var
  169.             time: longint;
  170.             timeH: t_targetH;
  171.  
  172.     begin
  173.         time := TickCount;
  174.         timeH := t_targetH(nshc_parms^.data);
  175.         if time > timeH^^ then
  176.             DelayStop(nshc_parms, nshc_calls);
  177.     end;
  178.  
  179. {  ================================================== }
  180.  
  181. { pascal 'main' for commands }
  182.  
  183.     procedure theCommand (nshc_parms: t_nshc_parms; nshc_calls: t_nshc_calls);
  184.         var
  185.             s: Str255;
  186.     begin
  187.  
  188.         if nshc_parms^.version <> nshc_version then
  189.             begin
  190.                 s := 'This command is not of a compatible version.';
  191.                 NSH_putStr_err(nshc_calls, s);
  192.                 NSH_putchar_err(nshc_calls, RETURN_CHAR);
  193.                 nshc_parms^.action := nsh_idle;
  194.                 nshc_parms^.result := NSHC_ERR_VERSION;
  195.             end
  196.         else
  197.             case nshc_parms^.action of
  198.                 nsh_start: 
  199.                     DelayStart(nshc_parms, nshc_calls);
  200.                 nsh_continue: 
  201.                     DelayContinue(nshc_parms, nshc_calls);
  202.                 nsh_stop: 
  203.                     DelayStop(nshc_parms, nshc_calls);
  204.             end;
  205.  
  206.     end;
  207.  
  208. end.